package com.example.rk.controller;

import com.example.rk.common.MyPageInfo;


import com.example.rk.common.SessionParameter;
import com.example.rk.entity.Admin;
import com.example.rk.entity.Changest;
import com.example.rk.entity.Company;
import com.example.rk.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/company")
public class CompanyController {
    public  static  final String FILE_STORE_PATH="D:/A/shuju/rk/src/main/resources/static/style/images/upload";
    @Autowired
    @Resource
    private CompanyService service;

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id,HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        service.deleteById(id);
        if(admin.getType().equals("公司")){
        return "redirect:/company/gslist";}else
        {
            return "redirect:/company/list";
        }
    }

    @RequestMapping("/doAdd")
    @ResponseBody
    public String doAdd(Company company,HttpServletRequest request)
    {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        if(admin.getType().equals("公司")){
        company.setCid(admin.getId());}
        return service.doAdd(company);
    }

    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv, HttpServletRequest request) {
        Company company = service.findById(id);
        mv.addObject("company", company);
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        if (admin.getType().equals("管理员")) {
            mv.setViewName("/gly/companyedit");
            return mv;
        } else if (admin.getType().equals("学生")) {
            mv.setViewName("/admin/main");//不对的之后改
            return mv;
        } else if (admin.getType().equals("公司")) {
            mv.setViewName("/gs/edit");
            return mv;
        } else {
            mv.setViewName("/job/add");
            return mv;
        }
    }

    @RequestMapping("/list")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo = service.findBySearch(request);
        //List<RenKo> courseList= renKoService.findAll();
        mv.addObject("searchname", request.getParameter("name"));
        mv.addObject("searchposition", request.getParameter("position"));
        mv.addObject("pageInfo", myPageInfo);
        if (admin.getType().equals("管理员")) {
            mv.setViewName("/gly/companylist");
            return mv;
        } else if (admin.getType().equals("学生")) {
            mv.setViewName("/xs/companylist");
            return mv;
        } else if (admin.getType().equals("公司")) {
            mv.setViewName("/gs/companylist");
            return mv;
        } else {
            mv.setViewName("/job/add");
            return mv;
        }
    }
    @RequestMapping("/gslist")
    public ModelAndView xslb(HttpServletRequest request, ModelAndView mv) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        mv.setViewName("/gs/companylist1");
        MyPageInfo myPageInfo = service.findBySearch1(request,admin.getId());
        //List<RenKo> courseList= renKoService.findAll();
        mv.addObject("searchname", request.getParameter("name"));
        mv.addObject("searchposition", request.getParameter("position"));
        mv.addObject("pageInfo", myPageInfo);
        return mv;

    }
    @RequestMapping("/doEdit")
    @ResponseBody

    public String doEdit(Company company) {
        return service.doEdit(company);
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestPart(name = "file", required = false) MultipartFile multipartFile,
                             HttpServletRequest request){
        Admin admin = (Admin)request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        String oldName = multipartFile.getOriginalFilename();  // aaabbb.jpg    12_2022-08-15.jpg
        String substring = oldName.substring(oldName.indexOf("."));  //.jpg
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(new Date());
        //构建真实的文件路径
        File fileStoePAth = new File(FILE_STORE_PATH);
        try {
            if (!fileStoePAth.exists())
                fileStoePAth.mkdirs() ;  //如果存储路径不存在，就创建
            File newFile = new File(FILE_STORE_PATH+ File.separator+admin.getId()+"-"+admin.getName()+"-"+format+substring);
            multipartFile.transferTo(newFile);
//            service.setImageUrl(newFile.getName(),id);
            return newFile.getName();
        } catch (IOException e) {
            return "error";
        }

    }
    @RequestMapping("/zigeadd")
    public String xslb1(Changest changest, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        changest.setCid(admin.getId());
         service.creattupian(changest);
        return "redirect:/admin/main";
    }
}



